<template>
  <!--员工-->
  <div class="jingli">
    <div class="header">
      <div>
        <wv-cell
          title=""
          :value="startTime"
          is-link
          @click="openPicker('beginDatePicker')"
        />
        <wv-datetime-picker
          type="date"
          ref="beginDatePicker"
          :startDate="minDate"
          :endDate="endDate"
          v-model="beginDate"
          @confirm="onConfirmBeginDate"
        />
        <div class="line"></div>
        <wv-cell
          title=""
          :value="endTime"
          is-link
          @click="openPicker('finshDatePicker')"
        />
        <wv-datetime-picker
          type="date"
          ref="finshDatePicker"
          :startDate="minDate"
          :endDate="endDate"
          v-model="finshDate"
          @confirm="onConfirmFinshDate"
        />
      </div>
    </div>
    <div class="page-infinite-wrapper" ref="wrapper" :style="{ height: wrapperHeight + 'px' }">
      <wv-group
        v-infinite-scroll="getList"
        infinite-scroll-disabled="loading"
        infinite-scroll-distance="10"
      >
        <list :achievementList="achievementList" v-on:changeActiveIndex="getChangeActiveIndex"></list>
      </wv-group>
    </div>
  </div>
</template>
<script type="text/ecmascript-6">
  import list from '../template/yeji'
  import {employeeAchievement} from '../../../test/unit/http'
  import {Toast} from 'we-vue'

  export default {
    name: 'yuangong',
    props: {},
    data() {
      return {
        minDate: new Date(),
        endDate: new Date(),
        beginDate: new Date(),
        finshDate: new Date(),
        achievementList: [],
        isVipCard: '',
        wrapperHeight: 0,
        page: 1,
        rows: 5,
        loading: false
      };
    },
    methods: {
      openPicker(ref) {
        this.$refs[ref].open()
      },
      getList() {
        let params = {
          startDate: this.startTime,
          endDate: this.endTime,
          isVipCard: this.isVipCard,
          page: this.page,
          rows: this.rows
        };
        this.getData(params)
      },
      getData(params) {
        this.loading = true;
        employeeAchievement(params).then(res => {
          console.log(res);
          if (res.code == 100) {
            let data = res.object;
            let list = data.rows;
            let pages = data.pages;
            this.pages = pages;
            let totalNum = data.totalNum;
            this.totalNum = totalNum;
            if (list.length > 0) {
              this.achievementList = this.achievementList.concat(list);
            }
            if (this.page < this.pages) {
              this.page++;
              this.loading = false
            } else {
              this.loading = true
            }
          }
        })
      },
      onConfirmBeginDate(value) {
        let message = '';
        if (value !== null && typeof value === 'object') {
          let year = value.getFullYear();
          let month = value.getMonth() + 1;
          if (month < 10) {
            month = '0' + month;
          }
          let date = value.getDate();
          if (date < 10) {
            date = '0' + date;
          }
          message = `${year}-${month}-${date}`
        } else {
          message = value
        }
        this.getList();
      },
      onConfirmFinshDate(value) {
        let message = '';
        if (value !== null && typeof value === 'object') {
          let year = value.getFullYear();
          let month = value.getMonth() + 1;
          if (month < 10) {
            month = '0' + month;
          }
          let date = value.getDate();
          if (date < 10) {
            date = '0' + date;
          }
          message = `${year}-${month}-${date}`
        } else {
          message = value
        }
        this.getList();
      },
      getChangeActiveIndex(activeIndex) {
        if (activeIndex == 0) {
          this.isVipCard = '';
        }
        if (activeIndex == 1) {
          this.isVipCard = activeIndex;
        }
        if (activeIndex == 2) {
          this.isVipCard = activeIndex;
        }
        this.getList();
      }
    },
    created() {
      this.minDate = this.$myUtils.computedMinDate(30);
    },
    computed: {
      startTime: {
        get: function () {
          var value = new Date(new Date().setDate(new Date().getDate() - 30));
          let message = '';
          if (value !== null && typeof value === 'object') {
            let year = value.getFullYear();
            let month = value.getMonth() + 1;
            if (month < 10) {
              month = '0' + month;
            }
            let date = value.getDate();
            if (date < 10) {
              date = '0' + date;
            }
            message = `${year}-${month}-${date}`;
          } else {
            message = value;
          }
          return message;
        },
        set: function (date) {
          return date;
        }
      },
      endTime: {
        get: function () {
          var value = this.finshDate;
          let message = '';
          if (value !== null && typeof value === 'object') {
            let year = value.getFullYear();
            let month = value.getMonth() + 1;
            if (month < 10) {
              month = '0' + month;
            }
            let date = value.getDate();
            if (date < 10) {
              date = '0' + date;
            }
            message = `${year}-${month}-${date}`;
          } else {
            message = value;
          }
          return message;
        },
        set: function (date) {
          return date;
        }
      }
    },
    components: {
      list
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less" rel="stylesheet/less">
  .header {

    padding: 10px;
    margin-bottom: 10px;
    border-bottom: 1px solid #ddd;

    > div {
      width: 200px;
      margin: 0 auto;
      display: flex;

    }

    .line {
      width: 24px;
      height: 1px;
      background-color: #ddd;
      content: '';
      display: block;
      margin: 19px 5px 0;
    }
  }

  .weui-cell:before {
    border-top: none;
  }

  .weui-cell_access {
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    color: inherit;
    border: 1px solid #ddd;
    padding: 7px 4px;
    border-radius: 5px;
  }

  .weui-cell_access:last-child {
    margin-right: 0;
  }
</style>
